package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.apache.catalina.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Result getPageResult(EmpQueryParam param) {
        //设置分页参数
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        LambdaQueryWrapper<Emp> lqw = new LambdaQueryWrapper<Emp>()
                //三个参数： 1：条件是否成立 true成立  false不成立   2：查询的列名   3：值
                //参数一为true时才会拼接该条件
                .like(StringUtils.isNotBlank(name), Emp::getName, name)
                .eq(ObjectUtils.isNotNull(gender), Emp::getGender, gender)
                .between(ObjectUtils.isNotNull(begin) && ObjectUtils.isNotNull(end), Emp::getEntryDate, begin, end);
        this.page(page, lqw);
        //获取所有员工的部门id
        //{1:"学工部",2"教研部"}
        List<Emp> emps = page.getRecords();
        Set<Integer> deptIds = emps.stream().map(Emp::getDeptId).collect(Collectors.toSet());
        List<Dept> depts = deptMapper.selectList(new LambdaQueryWrapper<Dept>().in(Dept::getId, deptIds)); //批量查询部门信息
        //{1：”学工部“,2:"教研部"}
        Map<Integer,String> deptMap=depts.stream().collect(Collectors.toMap(Dept::getId,Dept::getName));
        emps.forEach(v->v.setDeptName(deptMap.get(v.getDeptId())));
        PageResult<Emp> pageResult=new PageResult<>(page.getTotal(),page.getRecords());
        return Result.success(pageResult);
    }


    @Override
    @Transactional
    public boolean saveEmpAndExprList(Emp emp) {
        this.save(emp);
        //调用service层方法保存员工工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        //添加工作经历所属员工
        exprList.forEach(v->v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
        return  true;
    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        //工作经历列表
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,id)));
        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        //调用service层方法修改员工信息
        //根据id更新员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        //根据id批量删除员工的工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        //- 批量保存员工的工作经历信息
        exprList.forEach(v->v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    /**
     * 批量删除员工
     *
     * @param ids 要删除的员工id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmpsByIds(List<Integer> ids) {
        // 调用service层方法批量删除员工信息
        removeBatchByIds(ids);

        // 删除员工工作经历信息
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId,ids));

    }
}
